//
//  FlickManager.m
//  FliqFlicks
//
//  Created by EJ Lacap on 1/12/15.
//  Copyright (c) 2015 Lexnus. All rights reserved.
//

#import "FlickManager.h"
#import "Flick.h"
#import "MappingProvider.h"

static FlickManager *sharedManager = nil;

@implementation FlickManager

+ (instancetype)sharedManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [super sharedManager];
    });
    
    return sharedManager;
}

-(void) getSuggestedFlicksWithParams:(NSDictionary *) parameters success:(void (^)(NSMutableArray *flicks))success failure:(void (^)(RKObjectRequestOperation *operation, NSError *error))failure{
    
    [self getObjectsAtPath:@"discover/movie" parameters:parameters success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
    if (success) {
        NSMutableArray *flicks = [mappingResult.array mutableCopy];
        success(flicks);
    }
} failure:^(RKObjectRequestOperation *operation, NSError *error) {
    if (failure) {
        failure(operation, error);
    }
}];
}

#pragma mark - Setup Helpers

- (void) setupResponseDescriptors {
    [super setupResponseDescriptors];
    
    RKResponseDescriptor *getFlicksResponseDescriptors = [RKResponseDescriptor responseDescriptorWithMapping:[MappingProvider flickMapping] method:RKRequestMethodGET pathPattern:@"discover/movie" keyPath:@"results" statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];
    [self addResponseDescriptor:getFlicksResponseDescriptors];
}
@end
